//
//  WelcomeViewController.swift
//  微博个人swift
//
//  Created by 李灏 on 16/2/19.
//  Copyright © 2016年 lh. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class WelcomeViewController: UIViewController {

    override func loadView() {
        view = backGroundImage
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    private func setupUI(){
        view.addSubview(headImageView)
        view.addSubview(welcomeLabel)
        
        //自动布局
        headImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.centerY.equalTo(view.snp_bottom).offset(-200)
            make.size.equalTo(CGSizeMake(90, 90))
        }
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView.snp_centerX)
            make.centerY.equalTo(headImageView.snp_bottom).offset(20)
        }
        //切圆角
        headImageView.layer.cornerRadius = 45
        headImageView.layer.masksToBounds = true
        
        welcomeLabel.alpha = 0
        
        headImageView.sd_setImageWithURL(AccountViewModel.sharedAccountViewModel.headViewImage, placeholderImage: UIImage(named: "avatar_default_big"))
    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    private func startAnimation() {
        //1.usingSpringWithDamping: 弹簧系数  0.1 ~ 1  越小 越弹
        //2.initialSpringVelocity 加速度
        //options:动画可选项  OC按位枚举 swift: 数组
        
        //弹簧系数设置准则 (弹簧系数 * 10 ~= 加速度)
        let offset = -UIScreen.mainScreen().bounds.height + 200
        headImageView.snp_updateConstraints { (make) -> Void in
            make.centerY.equalTo(view.snp_bottom).offset(offset)
        }

        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.98, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            //刷新页面
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
            //显示文案
            UIView.animateWithDuration(0.6, animations: { () -> Void in
                self.welcomeLabel.alpha = 1.0
                }, completion: { (_) -> Void in
                //发出显示首页的通知
                NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVC, object: nil)
            })
        }
    }
    
    private lazy var backGroundImage:UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    private lazy var headImageView:UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    private lazy var welcomeLabel:UILabel = UILabel(text: (AccountViewModel.sharedAccountViewModel.userName ?? "")+"欢迎归来", fontSize: 18, textColor: UIColor.darkGrayColor())


}
